package com.cater.sys.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cater.annotation.SystemServiceLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.query.UserQuery;
import com.cater.sys.dao.UserDao;
import com.cater.sys.entity.User;

/**
 * @author sd 用户管理
 */
@Service("UserService")
public class UserService {

	@Autowired
	private UserDao userDao;

	/**
	 * 查询所有用户
	 * 
	 * @return
	 */
	@SystemServiceLog(description = "用户查询")
	public PageResult<User> findPageResult(UserQuery userQuery) {
		PageResult<User> result = new PageResult<User>();
		result.setPage(userQuery.getPage());
		result.setPageSize(userQuery.getRows());
		int count = userDao.findCount(userQuery);
		/*
		 * if (count == 0) { return result; }
		 */
		int first = (userQuery.getPage() - 1) * userQuery.getRows();
		int max = userQuery.getRows();
		List<User> list = userDao.findList(userQuery, first, max);
		result.setRows(list);
		result.setRecords(count);
		return result;
	}

	@SystemServiceLog(description = "查询全部用户")
	public List<User> getAll() {
		return userDao.getAll();
	}

	/**
	 * 根据用户名查询用户
	 * 
	 * @param loginName
	 * @return
	 */
	@SystemServiceLog(description = "用户名查询用户")
	public User findByName(String loginName) {
		return userDao.getByName(loginName);
	}

	@SystemServiceLog(description = "获取全部用户")
	public List<User> getAllCommon(CommonQuery commonQuery) {
		return userDao.getAllCommon(commonQuery);
	}

	@SystemServiceLog(description = "部门id查询用户")
	public List<User> getUserbydepid(Integer depid) {
		return userDao.getUserbydepid(depid);
	}

	/**
	 * 根据ID查询用户
	 * 
	 * @param id
	 * @return
	 */
	@SystemServiceLog(description = "用户ID查询用户")
	public User getByid(Integer id) {
		return userDao.getById(id);
	}

	/**
	 * 根据用户名和密码查询用户
	 * 
	 * @param loginName
	 * @param password
	 * @return
	 */
	@SystemServiceLog(description = "根据用户名和密码查询用户")
	public User findByNameandpwd(String loginName, String password) {
		return userDao.getByNameandpwd(loginName, password);
	}

	/**
	 * 修改用户
	 * 
	 * @param user
	 */
	@SystemServiceLog(description = "修改用户")
	public void update(User user) {
		userDao.update(user);
	}

	/**
	 * 添加用户
	 * 
	 * @param user
	 * @return
	 */
	@SystemServiceLog(description = "添加用户")
	public int save(User user) {
		return userDao.save(user);
	}

	@SystemServiceLog(description = "删除用户")
	public void deletestate(Integer id) {
		userDao.deletestate(id);
	}

	/**
	 * 根据用户ID和角色Id添加用户角色关联信息
	 * 
	 * @param userId
	 * @param roleId
	 */
	@SystemServiceLog(description = "添加用户角色对应关系")
	public void saveUR(Integer userId, Integer roleId) {
		userDao.saveUR(userId, roleId);
	};

	/**
	 * 根据用户ID删除角色用户关联表
	 * 
	 * @param userId
	 */
	@SystemServiceLog(description = "删除角色用户关系")
	public void deleteUR(Integer userId) {
		userDao.removeUR(userId);
	}

	/**
	 * 删除
	 * 
	 * @param userId
	 */
	@SystemServiceLog(description = "删除用户")
	public void delete(Integer userId) {
		userDao.delete(userId);
	};

	/**
	 * 修改密码
	 * 
	 * @param user
	 * @return
	 */
	public int updatepaw(User user) {
		return userDao.updatepaw(user);
	}

	public User getUserByCompanyId(Integer companyId) {
		List<User> list = userDao.getUserByCompanyId(companyId);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return new User();
	}

	public List<User> getAllUserIds(CommonQuery query) {
		return userDao.getAllUserIds(query);
	}

	public List<Map<String, String>> getByArrIds(String[] ids) {
		return userDao.getByArrIds(ids);
	}
}
